package cn.echo.modules.sys.controller.gradecontroller;

import cn.echo.modules.sys.entity.Grade;
import cn.echo.modules.sys.entity.User;
import cn.echo.modules.sys.service.GradeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/4/19.
 */
@Controller
@RequestMapping("/grade")
public class GradeController {
    @RequestMapping(value = "grade")
    public String showResult(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                             @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                             Model model, HttpServletRequest req,@RequestParam(required = false)String message){
        GradeService gradeService = new GradeService();
        PageHelper.startPage(pageNum,pageSize);
        Grade grade = new Grade();
        User user = new User();
        grade.setUser(user);
        Page<Grade> gradeList = (Page)gradeService.showResult(grade);
        //分页信息
        PageInfo<Grade> pageInfo = new PageInfo(gradeList);
        req.setAttribute("page",pageInfo);
        return "sys/grade/Grade";
    }

    @RequestMapping(value = "gradeShow")
    public String showresult(Model model, HttpServletRequest req){
        GradeService gradeService = new GradeService();
        Integer pageNum = Integer.valueOf(req.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum,pageSize);
        Page<Grade> gradeList = (Page)gradeService.showResult(new Grade());
        //分页信息
        PageInfo<Grade> pageInfo = new PageInfo(gradeList);
        req.setAttribute("page",pageInfo);
        return "sys/grade/Grade";
    }

    @RequestMapping("gradeDelete")
    public String delete(Grade grade){
        GradeService gradeService = new GradeService();
        gradeService.deleteById(grade);
        return "redirect:grade";
    }

    @RequestMapping("gradeEdit")
    public String edit(Grade grade){
        GradeService gradeService = new GradeService();
        gradeService.updateById(grade);
        return "redirect:grade";
    }

    @RequestMapping(value = "gradeInsert",method = RequestMethod.POST)
    public String add(Grade grade){
        GradeService gradeService = new GradeService();
        List<Grade> gradeList = gradeService.selectById(grade);
        if(gradeList.isEmpty()){
            gradeService.insert(grade);
            return "redirect:grade";
        }else{
            return "redirect:grade";
        }
    }

    @RequestMapping(value ="gradeSelectById",method = RequestMethod.POST)
    public String gradeSelect00(Grade grade,Model model){
        GradeService gradeService = new GradeService();
        List<Grade> gradeList = gradeService.showResult(grade);
        if(gradeList.size() <= 0){
            model.addAttribute("page","error");
        }else{
            model.addAttribute("page",gradeList);
        }
        return "sys/grade/Grade_select";
    }

    public List<Grade> gradeSelect0(Grade grade){
        List<Grade> gradeList =null;
        GradeService gradeService = new GradeService();
        gradeList = gradeService.showResult(grade);
        return gradeList;
    }

    @RequestMapping(value = "toGradeEdit",method = RequestMethod.GET)
    public String toGradeEdit(Grade grade, Model model){
        List<Grade> grades = gradeSelect0(grade);
        Grade grade1 = grades.get(0);
        model.addAttribute("grade1",grade1);
        return "sys/grade/Grade_edit";
    }


    @RequestMapping(value ="ToInsert",method = RequestMethod.GET)
    public String ToInsert(){
        return "sys/grade/Grade_insert";
    }


    @RequestMapping(value = "ToSelect",method = RequestMethod.GET)
    public String select(){
        return "sys/grade/Grade_select";
    }

}
